#include "message.hpp"
#include "macro.hpp"

void testRpcRequest()
{
    MyRpc::BaseMessage::ptr bmp = MyRpc::MessageFactory::create(MyRpc::MType::REQ_RPC);
    // 把爷爷类指针转化为子类指针
    MyRpc::RpcRequest::ptr rrp = std::dynamic_pointer_cast<MyRpc::RpcRequest>(bmp);

    Json::Value para;
    para["num1"] = 11;
    para["num2"] = 22;
    rrp->setMethod("Add");
    rrp->setParameters(para);
    bool flag = rrp->check();
    if(flag == false)
    {
        std::cout << "RpcRequest 填入数据失败!\n";
        return;
    }
    std::string str;
    rrp->serialize(str);
    std::cout << str << "\n";

    MyRpc::RpcRequest::ptr rrp1 = MyRpc::MessageFactory::create<MyRpc::RpcRequest>();
    rrp1->unserialize(str);
    flag = rrp1->check();
    if(flag == false)
    {
        std::cout << "RpcRequest 数据反序列化失败!\n";
        return;
    }
    std::cout << rrp1->method() << "\n";
    std::cout << rrp1->parameters()["num1"].asInt() << "\n";
    std::cout << rrp1->parameters()["num2"].asInt() << "\n";
}

void testTopicRequest()
{
    MyRpc::BaseMessage::ptr bmp = MyRpc::MessageFactory::create(MyRpc::MType::REQ_TOPIC);
    MyRpc::TopicRequest::ptr trp = std::dynamic_pointer_cast<MyRpc::TopicRequest>(bmp);

    trp->setMsg("hello world");
    trp->setTopicRequsetOpty(MyRpc::TopicRequestOpType::TOPIC_PUBLISH);
    trp->setKey("topic");
    
    std::string str;
    trp->serialize(str);
    bool flag = trp->check();
    if(flag == false)
    {
        std::cout << "TopicRequest 数据反序列失败\n";
        return;
    }

    std::cout << str << "\n";
    // std::cout << trp->msg() << "\n";
    // std::cout << (int)trp->topicRequsetOpty() << "\n";
    // std::cout << trp->key() << "\n";

    MyRpc::TopicRequest::ptr trp1 = MyRpc::MessageFactory::create<MyRpc::TopicRequest>();
    trp1->unserialize(str);
    flag = trp1->check();
    if(flag == false)
    {
        std::cout << "TopicRequest 数据反序列化失败\n";
        return;
    }

    std::cout << trp->msg() << "\n";
    std::cout << (int)trp->topicRequsetOpty() << "\n";
    std::cout << trp->key() << "\n";

}

void testServiceRequest()
{
    MyRpc::BaseMessage::ptr bmp = MyRpc::MessageFactory::create(MyRpc::MType::REQ_SERVICE);
    MyRpc::ServiceRequest::ptr srp = std::dynamic_pointer_cast<MyRpc::ServiceRequest>(bmp);

    srp->setOptype(MyRpc::ServiceRequestOpType::SERVICE_REGISTRY);
    srp->setMethod("注册服务主机");

    MyRpc::Addr addr;
    addr.first = "127.0.0.1";
    addr.second = 9090;
    srp->setHost(addr);
    
    std::string str;
    srp->serialize(str);
    bool flag = srp->check();
    if(flag == false)
    {
        std::cout << "TopicRequest 数据序列失败\n";
        return;
    }

    std::cout << str << "\n";

    MyRpc::ServiceRequest::ptr srp1 = MyRpc::MessageFactory::create<MyRpc::ServiceRequest>();
    srp1->unserialize(str);
    flag = srp1->check();
    if(flag == false)
    {
        std::cout << "TopicRequest 数据反序列化失败\n";
        return;
    }
    std::cout << srp1->method() << "\n";
    std::cout << srp1->host().first << " : " << srp1->host().second << "\n";
    std::cout << (int)srp1->optype() << "\n";
   
}

void testRpcResponse()
{
    MyRpc::BaseMessage::ptr bmp = MyRpc::MessageFactory::create(MyRpc::MType::RSP_RPC);
    MyRpc::RpcResponse::ptr rrp = std::dynamic_pointer_cast<MyRpc::RpcResponse>(bmp);

    rrp->setRcode(MyRpc::RCode::RCODE_OK);
    Json::Value res;
    res["result"] = 33;
    rrp->setResult(res);
    
    std::string str;
    rrp->serialize(str);
    bool flag = rrp->check();
    if(flag == false)
    {
        std::cout << "RpcResponse 数据序列失败\n";
        return;
    }

    std::cout << str << "\n";

    MyRpc::RpcResponse::ptr rrp1 = MyRpc::MessageFactory::create<MyRpc::RpcResponse>();
    rrp1->unserialize(str);
    flag = rrp1->check();
    if(flag == false)
    {
        std::cout << "RpcResponse 数据反序列化失败\n";
        return;
    }

    std::cout << MyRpc::errReason(rrp1->rcode()) << "\n";
    std::cout << rrp1->result()["result"].asInt() << "\n";
}

void testServiceResponse()
{
    MyRpc::BaseMessage::ptr bmp = MyRpc::MessageFactory::create(MyRpc::MType::RSP_SERVICE);
    MyRpc::ServiceResponse::ptr srp = std::dynamic_pointer_cast<MyRpc::ServiceResponse>(bmp);

    // 这里实现发现服务主机操作
    srp->setRcode(MyRpc::RCode::RCODE_OK);
    srp->setOptype(MyRpc::ServiceRequestOpType::SERVICE_DISCOVERY);
    srp->setMethod("发现主机服务");
    std::vector<MyRpc::Addr> addrs;
    addrs.push_back(std::make_pair("127.0.0.1", 9090));
    addrs.push_back(std::make_pair("127.0.0.2", 9091));
    
    srp->setHosts(addrs);
    std::string str;
    srp->serialize(str);
    bool flag = srp->check();
    if(flag == false)
    {
        std::cout << "ServiceResponse 数据序列失败\n";
        return;
    }

    std::cout << str << "\n";

    MyRpc::ServiceResponse::ptr srp1 = MyRpc::MessageFactory::create<MyRpc::ServiceResponse>();
    srp1->unserialize(str);
    flag = srp1->check();
    if(flag == false)
    {
        std::cout << "ServiceResponse 数据反序列化失败\n";
        return;
    }

    std::cout << srp1->method() << "\n";
    std::cout << (int)srp1->optype() << "\n";
    std::vector<MyRpc::Addr> tem = srp1->hosts();
    for(auto& e : tem)
    {
        std::cout << e.first << " : " << e.second << "\n";
    }

}

int main()
{
    // testRpcRequest();
    // testTopicRequest();

    // testServiceRequest();
    // testRpcResponse();
    testServiceResponse();
    return 0;
}